package com.glodon.paas.search.listener.impl;

import java.util.HashMap;
import java.util.Map;

import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.search.index.IndexService;
import com.glodon.paas.search.listener.IndexListener;
import com.glodon.paas.search.model.DocumentEvent;
import com.glodon.paas.search.model.Event;
import com.glodon.paas.search.model.IndexDocument;
import com.glodon.paas.search.util.DocWebService;
import com.glodon.paas.search.util.SearchConstants;

@Component("documentListener")
public class DocumentListenerImpl implements IndexListener {

    private Logger logger = LoggerFactory.getLogger(DocumentListenerImpl.class);
    @Autowired
    private IndexService indexService;
    @Autowired
    private DocWebService docService;
    @Autowired
    private Mapper dozerMapper;

    private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor("document-index");

    @Override
    public void onEvent(final Event event) {
        if (!(event instanceof DocumentEvent)) {
            logger.warn("event is not DocumentEvent Class type.");
            return;
        }
        taskExecutor.execute(new Runnable() {
            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void run() {
                DocumentEvent docEvent = (DocumentEvent) event;
                String fileId = docEvent.getFileId();
                switch (docEvent.getEventType()) {
                case RENAME: {

                }
                case MOVE: {
                    File fileMeta = docService.getFileMeta(fileId, docEvent.getOwnerId());
                    Map changedParams = new HashMap();
                    if (fileMeta.getExtension() != null) {// 恢复文件带后缀的名称
                        changedParams.put("name", fileMeta.getName() + "." + fileMeta.getExtension());
                    } else {
                        changedParams.put("name", fileMeta.getName());
                    }
                    changedParams.put("ownerId", fileMeta.getOwnerId());
                    changedParams.put("updateTime", fileMeta.getUpdateTime());
                    // changedParams.put("updator", fileMeta.getUpdator());
                    indexService.update(SearchConstants.INDEX_PAAS, SearchConstants.TYPE_DOC, fileId, changedParams);
                    break;
                }
                case UPLOAD: {
                    File fileMeta = docService.getFileMeta(fileId, docEvent.getOwnerId());
                    IndexDocument doc = dozerMapper.map(fileMeta, IndexDocument.class);
                    if (doc.getExtension() != null) {// 恢复文件带后缀的名称
                        doc.setName(doc.getName() + "." + doc.getExtension());
                    }
                    indexService.index(SearchConstants.INDEX_PAAS, SearchConstants.TYPE_DOC, doc);
                    break;
                }
                case DELETE: {
                    indexService.delete(SearchConstants.INDEX_PAAS, SearchConstants.TYPE_DOC, fileId);
                    break;
                }
                default:
                    break;
                }
            }
        });
    }

    public void setIndexService(IndexService indexService) {
        this.indexService = indexService;
    }

    public void setDocService(DocWebService docService) {
        this.docService = docService;
    }
}
